package office.action.business;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import office.action.business.form.BidBookConditionForm;
import office.action.business.form.ContractConditionForm;
import office.action.business.form.ContractForm;
import office.action.business.wrapper.BidBookWrapper;
import office.action.business.wrapper.ContractWrapper;
import office.service.business.IBidBookService;
import office.service.business.IContractService;
import platform.common.action.BaseAction;
import platform.common.bo.Page;
import platform.util.Util;
import current.security.user.domain.User;
import current.security.user.manager.service.UserManagerService;

/**
 * 
 * 合同管理
 * 
 * @author ZhangYQ
 * 
 */
public class ContractAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private IContractService contractService;
	private IBidBookService bidBookService;
	@Resource
	private UserManagerService userManagerService;
	private Page page; // 分页信息
	private List<ContractWrapper> wrapperList;
	private List<User> userList = new ArrayList<User>();
	private List<BidBookWrapper> bidBookWrapperList;
	private ContractForm form;
	private ContractConditionForm condition;
	private BidBookConditionForm bidBookCondition;
	private String id;

	/**
	 * 获取所有的合同以及根据条件进行查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findAllContract() throws Exception {
		String result = "listContractByPage";

		if (null == condition) {
			result = "findAllContract";
			condition = new ContractConditionForm();
		}
		if (id != null) {
			condition.setCreateMan(id);
		} else {
			// condition.setCreateMan((String)getUserMap().get("userId"));
		}
		condition.setContractType(Util.parsePropertiesReturnStr("office",
				"Contract"));
		page = contractService.getPageInfo(condition);
		wrapperList = contractService.findAllContract(condition, page);
		// 根据部门Id获取所有的下级人员
		/*
		 * if(BusiUtil.validateRole(getUserRoleName())){ userList =
		 * userManagerService.getUsersByAllDepartment(getUserDepartmentId()); }
		 */
		// 获取销售人员接口待实现
		userList = userManagerService.getUserByRoleAndCompany("xsry,qyjl,xsjl",
				getUserCompanyId());
		return result;
	}

	/**
	 * 初始化编辑合同页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String gotoEditContract() throws Exception {
		if (null != id && !"".equals(id)) {
			form = contractService.getById(id);
		}
		bidBookWrapperList = bidBookService
				.findAllBidBookByStatus(bidBookCondition);
		// 获取销售人员接口待实现
		userList = userManagerService.getUserByRoleAndCompany("xsry,qyjl,xsjl",
				getUserCompanyId());
		return "gotoEditContract";
	}

	/**
	 * 编辑合同
	 * 
	 * @return
	 * @throws Exception
	 */
	public String editContract() throws Exception {
		try {
			// 获取当前用户
			String userId = (String) getUserMap().get("userId");
			form.setCreateMan(userId);
			contractService.editContract(form);
			if (null == form.getId() || ("").equals(form.getId())) {
				this.getAjaxMessagesJson().setMessage(true, "", "新建成功");
			} else {
				this.getAjaxMessagesJson().setMessage(true, "", "编辑成功");
			}

		} catch (Exception e) {
			this.handleException(e);
		}
		return RESULT_AJAXJSON;
	}

	public String gotoLookContract() throws Exception {
		try {
			if (null != id && !"".equals(id)) {
				form = contractService.getById(id);
			}
		} catch (Exception e) {
			this.handleException(e);
		}
		return "gotoLookContract";
	}

	/**
	 * 
	 * <br/>
	 * 方法描述:分页条件查询合同
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listContractByPage() throws Exception {
		try {
			/*if (null == condition || (Util.isNull(condition.getProjectUnit()))) {
				condition = new ContractConditionForm();
			} else {
				if (condition.getCreateMan() == null) {
					condition.setCreateMan((String) getUserMap().get("userId"));
				}
			}*/
			condition.setContractType(Util.parsePropertiesReturnStr("office",
					"Contract"));
			wrapperList = contractService.findAllContract(condition, page);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		return "listContractByPage";
	}


	/**
	 * 初始化合同列表操作菜单
	 * 
	 * @return
	 * @throws Exception
	 */

	public String gotoContractMenu() throws Exception {
		form = contractService.getById(id);
		return "gotoContractMenu";
	}

	/**
	 * 跳转到合同删除确认页面
	 */
	public String gotoDeleteContract() {
		return "gotoDeleteContract";
	}

	/**
	 * 删除合同
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delContract() throws Exception {
		try {
			contractService.delContractById(id);
			this.getAjaxMessagesJson().setMessage(true, "", "操作成功");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		return RESULT_AJAXJSON;
	}

	/**
	 * 初始化签合同
	 * 
	 * @return
	 * @throws Exception
	 */
	public String gotoSignContract() throws Exception {
		return "gotoSignContract";
	}

	/**
	 * 签合同
	 * 
	 * @return
	 * @throws Exception
	 * @throws Exception
	 */
	public String signContract() {
		try {
			contractService.signContract(id);
			this.getAjaxMessagesJson().setMessage(true, "", "签署成功");
		} catch (Exception e) {
			this.getAjaxMessagesJson().setMessage(false, "", "签合同失败");
			log.error(e.getMessage(), e);
		}
		return RESULT_AJAXJSON;
	}

	/**
	 * 初始化回款页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String gotoBackMoneyContract() throws Exception {
		return "gotoBackMoneyContract";
	}

	/**
	 * 已回款
	 * 
	 * @return
	 * @throws Exception
	 * @throws Exception
	 */
	public String backMoneyContract() {
		try {
			contractService.backMoneyContract(id);
			this.getAjaxMessagesJson().setMessage(true, "", "合同回款成功");
		} catch (Exception e) {
			this.getAjaxMessagesJson().setMessage(false, "", "合同回款失败");
			log.error(e.getMessage(), e);
		}
		return RESULT_AJAXJSON;
	}

	/**
	 * 初始化开发票
	 * 
	 * @return
	 * @throws Exception
	 */
	public String gotoInvoiceContract() throws Exception {
		return "gotoInvoiceContract";
	}

	/**
	 * 开发票
	 * 
	 * @return
	 * @throws Exception
	 * @throws Exception
	 */
	public String invoiceContract() {
		try {
			contractService.invoiceGoodsOrder(id);
			this.getAjaxMessagesJson().setMessage(true, "", "新建成功");
		} catch (Exception e) {
			this.getAjaxMessagesJson().setMessage(false, "", "开发票失败");
			log.error(e.getMessage(), e);
		}
		return RESULT_AJAXJSON;
	}

	public IContractService getContractService() {
		return contractService;
	}

	public void setContractService(IContractService contractService) {
		this.contractService = contractService;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<ContractWrapper> getWrapperList() {
		return wrapperList;
	}

	public void setWrapperList(List<ContractWrapper> wrapperList) {
		this.wrapperList = wrapperList;
	}

	public ContractForm getForm() {
		return form;
	}

	public void setForm(ContractForm form) {
		this.form = form;
	}

	public ContractConditionForm getCondition() {
		return condition;
	}

	public void setCondition(ContractConditionForm condition) {
		this.condition = condition;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<BidBookWrapper> getBidBookWrapperList() {
		return bidBookWrapperList;
	}

	public void setBidBookWrapperList(List<BidBookWrapper> bidBookWrapperList) {
		this.bidBookWrapperList = bidBookWrapperList;
	}

	public IBidBookService getBidBookService() {
		return bidBookService;
	}

	public void setBidBookService(IBidBookService bidBookService) {
		this.bidBookService = bidBookService;
	}

	public BidBookConditionForm getBidBookCondition() {
		return bidBookCondition;
	}

	public void setBidBookCondition(BidBookConditionForm bidBookCondition) {
		this.bidBookCondition = bidBookCondition;
	}

	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

}
